# call是一个内置于make的函数，call会扩展他的第一个参数并把其余参数依次替换到 $1,$2... c
# $(function-name arg1[, argn])第一个参数前导空格会被删除，后续的都会被保存

define test
	echo "$1 @$2"
endef

all:
	@$(call   test,hello, world)

# 字符串函数
words = he the hen other
filter-test:
	@echo $(filter %he,$(words))
files = 1.c 2.c 1.l 1.h 2.h
filter-out-test:
	@echo $(filter-out %.h,$(files))
strings = hello world%
find-string:
	@echo $(findstring world%,$(strings))
files = 1.c 2.c 3.c car.cdr.c
subst:
	@echo $(subst .c,.o,$(files))
patsubst:
	@echo $(patsubst %.c,%.o,$(files))
word-test:
	@echo $(words $(files))
	@echo $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
	@echo $(firstword $(files))
	@echo $(wordlist 2,4,$(files))

# 杂项函数
sort:
	@echo $(sort b   c   a  d e g  e  )
xxx = hello world
shell-test:
	@echo $(shell echo $(xxx))

# 文件名函数
wildcard:
	@echo $(wildcard *file)
files = /aaa/bbb/1.c /xx/2.c
dir:
	@echo $(dir $(files))
	@echo $(notdir $(files))
suffix:
	@echo $(suffix $(files))
	@echo $(basename $(files))
	@echo $(addsuffix .o,$(files))
	@echo $(addprefix .o,$(files))
files = 1.c 2.c
dirs = /aaa/ /bbb/
join:
	@echo $(join $(dirs),$(files))

# 流程控制
# $(if cond,then,else) cond扩展后包含任何字符即使是空格，则为真
# 求值会移除cond前后空格
PATH_SEP = $(if $(COMSPEC),;,:) # ;为shell语句分割符
if-test:
	@echo $(PATH_SEP)
	@$(call assert,$($1),xxxx)
define assert
	$(if $1,,$(error Assert failed: $2))
endef

letters := $(foreach letter,a b c d,$(letter))
show-words:
	@echo $(letters)

# 杂项函数
# strip 移除前后空格，并替换多余空格
# origin 返回变量来自何处的字符串
test = "  hello  aaa bb vv   dd  "
strip-test:
	@echo $(strip $(test))
	@echo $(origin test)

# eval 将文本直接放入make解析器
$(eval sources := 1.c 2.c)
define program-var
	$1_xxx = $(filter %.c,$2)
endef
$(eval $(call program-var,aa,aa.c bb.a))
eval-test:
	@echo $(aa_xxx)
